//package medicalApp.velociraptors.com;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

/**
 * JDialog allowing the lookup of users
 * @author Lsun
 *
 */

@SuppressWarnings("serial")
public class LookupUser extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtFirstName;
	private JTextField txtLastName;
	private JTextField txtSSN;
	private JCheckBox chckbxSocialSecurityName;
	private JCheckBox chckbxLastName;
	private JCheckBox chkFirstName;
	protected boolean boolFirstName;
	protected boolean boolLastName;
	protected boolean boolSSN;
	private MedAppController mac;
	protected String fName;
	protected String lName;
	protected int socialSN;
	private int SSN;

	/**
	 * Constructor for the dialog.
	 * @param mac Medical Application Controller class
	 * @param drSSN Current Dr's SSN
	 */
	public LookupUser(final MedAppController mac, final int SSN) {
		setTitle("Lookup User");
		this.SSN = SSN;
		this.mac = mac;
		setResizable(false);
		setBounds(100, 100, 521, 242);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		chkFirstName = new JCheckBox("First Name");
		chkFirstName.addActionListener(new ActionListener() {
			/**
			 * Sets the first name field to enabled, allowing search on first name
			 */
			public void actionPerformed(ActionEvent arg0) {
				JCheckBox target = (JCheckBox)arg0.getSource();
				boolFirstName = target.isSelected();				
				txtFirstName.setEnabled(target.isSelected());
				if(!target.isSelected()){
					txtFirstName.setText("");
				}
				
			}
		});
		chkFirstName.setBounds(73, 61, 97, 23);
		contentPanel.add(chkFirstName);
		
		chckbxLastName = new JCheckBox("Last Name");
		chckbxLastName.addActionListener(new ActionListener() {
			/**
			 * Sets the last name field to enabled, allowing search on first name
			 */
			public void actionPerformed(ActionEvent arg0) {
				JCheckBox target = (JCheckBox)arg0.getSource();
				boolLastName = target.isSelected();
				txtLastName.setEnabled(target.isSelected());
				if(!target.isSelected()){
					txtLastName.setText("");
				}
			}
		});
		chckbxLastName.setBounds(73, 87, 97, 23);
		contentPanel.add(chckbxLastName);
		
		chckbxSocialSecurityName = new JCheckBox("Social Security Number");
		chckbxSocialSecurityName.addActionListener(new ActionListener() {
			/**
			 * Sets the first SSN to enabled, allowing search on first name
			 */
			public void actionPerformed(ActionEvent arg0) {
				JCheckBox target = (JCheckBox)arg0.getSource();
				boolSSN= target.isSelected();
				txtSSN.setEnabled(target.isSelected());
				if(!target.isSelected()){
					txtSSN.setText("");
				}
			}
		});
		chckbxSocialSecurityName.setBounds(73, 113, 171, 23);
		contentPanel.add(chckbxSocialSecurityName);
		
		txtFirstName = new JTextField();
		txtFirstName.setEnabled(false);
		txtFirstName.setBounds(250, 61, 219, 22);
		contentPanel.add(txtFirstName);
		txtFirstName.setColumns(10);
		
		txtLastName = new JTextField();
		txtLastName.setEnabled(false);
		txtLastName.setBounds(250, 87, 219, 22);
		contentPanel.add(txtLastName);
		txtLastName.setColumns(10);
		
		txtSSN = new JTextField();
		txtSSN.setEnabled(false);
		txtSSN.setBounds(250, 113, 219, 22);
		contentPanel.add(txtSSN);
		txtSSN.setColumns(10);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btnSearch = new JButton("Search");
				btnSearch.addActionListener(new ActionListener() {
					/**
					 * Searches the database for the specified parameters
					 */
					public void actionPerformed(ActionEvent arg0) {
						fName = txtFirstName.getText();
						mac.out.println("fName: "+fName);
						
						lName = txtLastName.getText();
						mac.out.println("lName: "+lName);
						
						if(txtSSN.isEnabled())
							socialSN = Integer.parseInt(txtSSN.getText());
						else
							socialSN =0;
						
						mac.out.println("SSN: "+socialSN);
						search();
					}
				});
				btnSearch.setActionCommand("OK");
				buttonPane.add(btnSearch);
				getRootPane().setDefaultButton(btnSearch);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					/**
					 * closes the dialog
					 */
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	/**
	 * Completes the search
	 */
	private void search(){
		UserLookupResults plr = new UserLookupResults(mac,SSN);
		plr.setFirstName(fName);
		plr.setLastName(lName);
		plr.setSocialSN(socialSN);
		plr.setTableModel(plr.myModel());
		plr.setVisible(true);
	}
}
